package heart

import (
	"context"
	"game_server/framework/global"
	"game_server/framework/message"
	"game_server/framework/task"
	"game_server/proto"
	"time"

	"github.com/duke-git/lancet/v2/convertor"
	"github.com/spf13/viper"
)

func HeartHandler(taskWrapper *task.TaskWrapper) *task.TaskWrapper {
	defer func() {
		err := recover()
		if err != nil {
			global.Logger.Errorf("err:%v", err)
		}
	}()
	// tempExitRoomValue := convertor.ToString(taskWrapper.ReceiverId)
	// exists := global.RedisClient.Exists(context.Background(), "game_server_temp_"+tempExitRoomValue).Val()
	// if exists != 1 {
	// 	tokenTime := viper.GetInt("redis.token-time")
	// 	global.RedisClient.Expire(context.Background(), viper.GetString("redis.player-key")+convertor.ToString(taskWrapper.ReceiverId), time.Duration(tokenTime)*time.Minute)
	// }
	tokenTime := viper.GetInt("redis.token-time")
	global.RedisClient.Expire(context.Background(), viper.GetString("redis.player-key")+convertor.ToString(taskWrapper.ReceiverId), time.Duration(tokenTime)*time.Minute)
	data := "PONG"
	messageWrapper := &message.MessageWrapper{
		Code: proto.HEART,
		Data: data,
	}
	senderIds := []int64{taskWrapper.ReceiverId}
	task := &task.TaskWrapper{
		ReceiverId:     taskWrapper.ReceiverId,
		MessageWrapper: messageWrapper,
		SenderIds:      senderIds,
	}
	return task
}
